import * as fs from 'fs';
import * as path from 'path';

import { BaseCreater, Template } from '../baseCreater';
import { ICreater } from '../iCreater';

import { Util } from '../../Util';


export class ComponentCreator extends BaseCreater implements ICreater {


    constructor() {
        let tmp = {
            ts: `
            import { Component, Injector, AfterViewInit } from '@angular/core';
            import { AppComponentBase } from '@shared/common/app-component-base';
            import { appModuleAnimation } from '@shared/animations/routerTransition';
            
            @Component({
                selector:'{0}',
                templateUrl:\`./{0}.component.html\`,
                styleUrls:['./{0}.component.less'],
                animations: [appModuleAnimation()]
            })
            export class {1}Component extends AppComponentBase implements AfterViewInit {
            
                constructor(i: Injector) {
                    super(i);
                }
            
                ngAfterViewInit(): void {
            
                }
            }
            `,
            html:`
                <div [@routerTransition]></div>
            `
        };
        super(tmp);
    }

    create(args: any) {
        let self = this;

        let componentName = <string>args.componentName;
        let filePath = path.join(args.filePath, Util.underscoreName(componentName));

        self.template.ts = Util.formatString(self.template.ts, [Util.underscoreName(componentName), Util.camelName(componentName)]);
        self.template.html = self.template.html;
        //1 创建提示文件
        let tsFileName = filePath + '.component.ts';
        self.createFile(tsFileName, self.template.ts).then(() => {
            console.log('创建ts文件成功');
        });

        //2 创建HTML文件
        let htmlFilePath = filePath + '.component.html';
        self.createFile(htmlFilePath, self.template.html).then(() => {
            console.log('创建html文件成功');
        });
        //3 创建样式文件
        self.createFile(filePath + '.component.less').then(() => {
            console.log('创建less文件成功');
        });

    }
}